<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class spread_uqcms extends control
{
    function __construct()
    {
        $this->uid = $_SESSION['user']['uid'];
        parent::__construct();
    }
    function index()
    {
        $disRow = $this->db->get_one("select id,price from " . table('distribute') . " where uid = " . $this->uid);
        if (!$disRow['id']) {
            $insert_id = $this->db->add(table('distribute'), array('uid' => $this->uid));
            $disRow['id'] = $insert_id;
            $disRow['price'] = '0';
        }
        $this->assign('disRow', $disRow);
        $account_price = $this->module('distribute')->get_count();
        $this->assign('account_price', $account_price);
        $this->display();
    }
    function detail()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $sql = "select * from " . table('distribute_bill') . " where uid = " . $this->uid . " and type = 2 order by id desc limit " . ($page - 1) * $page_size . "," . $page_size;
        $result = $this->db->get_all($sql);
        if ($result) {
            $data['list'] = $result;
            $total = $this->db->get_one("select count(id) as total from " . table('distribute_bill'));
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function settle()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $sql = "select * from " . table('distribute_settle') . " where uid = " . $this->uid . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size;
        $list = $this->db->get_all($sql);
        if ($list) {
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('distribute_settle') . " where uid = " . $this->uid);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function withdraw_to_account()
    {
        $userRow = $this->db->get_one("select uid,price from " . table('distribute') . " where uid = " . $this->uid);
        $price = $userRow['price'];
        if ($price > '0') {
            $this->db->autocommit();
            $query = $this->db->query("update " . table('distribute') . " set price = price - " . $price . " where uid = " . $this->uid);
            $query2 = $this->db->query("update " . table('member') . " set price=price + " . $price . " where uid = " . $this->uid);
            $wArr['uid'] = $this->uid;
            $wArr['name'] = '推广提现';
            $wArr['price'] = $price;
            $wArr['pay_io'] = '1';
            $wArr['pay_status'] = time();
            $wArr['pay_time'] = time();
            $wArr['addtime'] = time();
            $query3 = $this->db->add(table('member_bill'), $wArr);
            if ($query && $query2 && $query3) {
                $this->db->commit();
                showmsg('提现成功');
            } else {
                $this->db->rollback();
                showmsg('提现失败');
            }
        } else {
            showmsg('余额不足');
        }
    }
}